// PipeTable.cpp: implementation of the PipeTable class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PipeTable.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
#define GET_FUNCTION(A,C,B) \
	if( !this->m_pCurRecord ) \
	{ \
		return this->m_ResPtr->A( _T(C) );\
	}\
	else\
	{\
		return this->m_pCurRecord->B();\
	}

using namespace PDMS::PDMSDBIF;

BEGIN_REGISTER_MAP( PipeTable, MultiRecordsDatabaseElement)
			REGISTER( _T("PipeData"), GET_REGISTER( DatabaseElementFactory, Design_VersionOne ) )
END_REGISTER_MAP();


PipeTable::PipeTable()
{

}

PipeTable::~PipeTable()
{

}

void PipeTable::SetBuildFlag( BOOL bBuildFlag )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetBuildFlag( bBuildFlag );
}

void PipeTable::SetShopFlag( BOOL bShopFlag )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetShopFlag( bShopFlag );
}

void PipeTable::SetCaseRef( const StdString& strCaseRef )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetCaseRef( strCaseRef );
}

void PipeTable::SetDescription( const StdString& strDescription )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetDescription( strDescription );
}

void PipeTable::SetFluRef( const StdString& strFluRef )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetFluRef( strFluRef );
}

void PipeTable::SetFunction( const StdString& strFunction )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetFunction( strFunction );
}

void PipeTable::SetIspec( const StdString& strIspec )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetIspec( strIspec );
}

void PipeTable::SetMatRef( const StdString& strMatRef )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetMatRef( strMatRef );
}

void PipeTable::SetPipeBore( LengthUnit dPipeBore )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetPipeBore( dPipeBore );
}

void PipeTable::SetPressure( PressureUnit pressure )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetPressure( pressure );
}

void PipeTable::SetPspec( const StdString& strPspec )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetPspec( strPspec );
}

void PipeTable::SetPtspec( const StdString& strPtspec )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetPtspec( strPtspec );
}

void PipeTable::SetPurpose( const StdString& strPurpose )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetPurpose( strPurpose );
}

void PipeTable::SetTemperature( TemperatureUnit temperature )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetTemperature( temperature );
}

void PipeTable::SetTspec( const StdString& strTspec )
{
	ASSERT( m_pCurRecord != NULL );
	m_pCurRecord->SetTspec( strTspec );
}

BOOL PipeTable::GetBuildFlag()
{
	GET_FUNCTION( GetBool, "BuildFlag", GetBuildFlag )
}

BOOL PipeTable::GetShopFlag()
{
	GET_FUNCTION( GetBool, "ShopFlag", GetShopFlag )
}

StdString PipeTable::GetCaseRef()
{
	GET_FUNCTION( GetString, "CaseRef", GetCaseRef )
}

StdString PipeTable::GetDescription()
{
	GET_FUNCTION( GetString, "Description", GetDescription )
}

StdString PipeTable::GetPurpose()
{
	GET_FUNCTION( GetString, "Purpose", GetPurpose )
}

StdString PipeTable::GetFluRef()
{
	GET_FUNCTION( GetString, "FluRef", GetFluRef )
}

StdString PipeTable::GetFunction()
{
	GET_FUNCTION( GetString, "Function", GetFunction )
}

StdString PipeTable::GetIspec()
{
	GET_FUNCTION( GetString, "Ispec", GetIspec )
}

StdString PipeTable::GetMatRef()
{
	GET_FUNCTION( GetString, "Matref", GetMatRef )
}

LengthUnit PipeTable::GetPipeBore()
{
	double dbore;
	if ( m_pCurRecord == NULL )
	{
		dbore = m_ResPtr->GetDouble( _T("PipeBore") );
	}
	else
	{
		return m_pCurRecord->GetPipeBore();
	}
	return LengthUnit( dbore );
}

PressureUnit PipeTable::GetPressure()
{
	double pres;
	if( m_pCurRecord == NULL ) 
	{ 
		pres = m_ResPtr->GetDouble( "Pressure" );
	}
	else
	{
		return m_pCurRecord->GetPressure();
	}
	return PressureUnit( pres );
}

StdString PipeTable::GetPspec()
{
	GET_FUNCTION( GetString, "Pspec", GetPspec )
}

StdString PipeTable::GetPtspec()
{
	GET_FUNCTION( GetString, "Ptspec", GetPtspec )
}

StdString PipeTable::GetTspec()
{
	GET_FUNCTION( GetString, "Tspec", GetTspec )
}

unsigned long PipeTable::GetID(){
	GET_FUNCTION( GetLong, "ID", GetID );
}

unsigned long PipeTable::GetParentID(){
	GET_FUNCTION( GetLong, "ParentID", GetParentID );
}

TemperatureUnit PipeTable::GetTemperature()
{
	double temp;
	if( m_pCurRecord == NULL ) 
	{ 
		temp = m_ResPtr->GetDouble( "Temperature" );
	}
	else
	{
		return m_pCurRecord->GetTemperature();
	}
	return TemperatureUnit( temp );
}

StdString PipeTable::GetOpenSQL( const StdString& strCondition )
{
	StdString strTemp( "SELECT * FROM PipeData" );

	if ( ALLRECORD == strCondition )
	{
		return strTemp;
	}

	return strTemp + _T(" WHERE ") + strCondition;
}

StdString PipeTable::GetTableName()
{
	return _T("PipeData");
}

void PipeTable::SetData( RecordPtr_ ptr )
{
	ASSERT( m_ResPtr != NULL );

	m_ResPtr->SetLong( _T("ID"), ptr->GetID() );
	m_ResPtr->SetBool( _T("BuildFlag"), ptr->GetBuildFlag() ? true : false );
	m_ResPtr->SetBool( _T("ShopFlag"), ptr->GetShopFlag() ? true : false );
	m_ResPtr->SetDouble( _T("PipeBore"), ptr->GetPipeBore().GetValue() );
	m_ResPtr->SetDouble( _T("Pressure"), ptr->GetPressure().GetValue() );
	m_ResPtr->SetString( _T("CaseRef"), ptr->GetCaseRef() );
	m_ResPtr->SetString( _T("Description"), ptr->GetDescription() );
	m_ResPtr->SetString( _T("FluRef"), ptr->GetFluRef() );
	m_ResPtr->SetString( _T("Function"), ptr->GetFunction() );
	m_ResPtr->SetString( _T("Ispec"), ptr->GetIspec() );
	m_ResPtr->SetString( _T("Purpose"), ptr->GetPurpose() );
	m_ResPtr->SetString( _T("Tspec"), ptr->GetTspec() );
	m_ResPtr->SetString( _T("Ptspec"), ptr->GetPtspec() );
	m_ResPtr->SetDouble( _T("Temperature"), ptr->GetTemperature().GetValue() );
	m_ResPtr->SetLong( _T("ParentID"), ptr->GetParentID() );
	m_ResPtr->SetString( _T("Matref"), ptr->GetMatRef() );
	m_ResPtr->SetString( _T("Pspec"), ptr->GetPspec() );
}